﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using System.IO;

namespace MobileShop
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageMap = string.Format("Register[{0}]", this.ResolveUrl("~/Register.aspx"));
            }
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            lblError.Visible = false;
            ccJoin.ValidateCaptcha(txtCode.Text);
            if (!ccJoin.UserValidated)
            {
                txtCode.Text = "";
                lblError.Text = "Invalid validation code.";
                lblError.Visible = true;
                return;
            }

            try
            {
                if (txtEmail.Text != "" && Customers.GetCustomerByEmail(txtEmail.Text).RowCount > 0)
                {
                    lblError.Text = "This email has been used.";
                    lblError.Visible = true;
                    txtCode.Text = "";
                    return;
                }

                Customers cust = new Customers();
                cust.AddNew();
                cust.Deleted = false;
                cust.TotalAmount = 0;
                cust.PaidAmount = 0;
                cust.CustomerType = int.Parse(POSSystem.BLL.Utilities.GetConfigAppSetting("OnlineCustomer"));
                cust.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                cust.CustomerName = txtName.Text;
                cust.Address = txtAddress.Text;
                cust.Phone = txtPhone.Text;
                cust.Mobile = txtPhone.Text;
                cust.Email = txtEmail.Text;
                cust.Company = "";
                cust.Fax = "";
                cust.Password = POSSystem.BLL.Utilities.CreateMD5Hash(txtPassword.Text);
                cust.BranchID = Branches.CurrentBranchID;
                cust.Company = txtCompany.Text;
                cust.ABN = txtABN.Text;
                cust.Save();

                txtName.ReadOnly = true;
                txtAddress.ReadOnly = true;
                txtPhone.ReadOnly = true;
                txtCode.ReadOnly = true;
                txtEmail.ReadOnly = true;
                btnRegister.Enabled = false;

                try
                {
                    string tempFile = Server.MapPath("~/MailTemplates/CustomerRegister.htm");
                    string content = File.ReadAllText(tempFile);
                    content = string.Format(content, cust.s_CustomerName, cust.s_Email, cust.s_Phone);
                    string recipients = POSSystem.BLL.Utilities.GetConfigAppSetting("AutoMailRecipients");
                    POSSystem.BLL.Utilities.SendEmail(recipients, "New Customer", content);
                }
                catch (Exception ex)
                {
                    ErrorLog.WriteToFile("New Customer", ex.Message, ex.StackTrace);
                }

                divLogin.Visible = true;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("EditCustomerCtrl.ascx.cs", "lnkRegiester_OnClick", ex.Message + ex.StackTrace);
            }
        }
    }
}